
#ifndef _OV2640_PORT_H_
#define _OV2640_PORT_H_

#include <stm32h7xx_hal.h>

#define OV2640_DCMI_CLK_ENABLE() __HAL_RCC_DCMI_CLK_ENABLE()
#define OV2640_DCMI_IRQHandler DCMI_IRQHandler

#define OV2640_DMA_CLK_ENABLE() __HAL_RCC_DMA2_CLK_ENABLE()
#define OV2640_DMA_CLK_DISABLE() __HAL_RCC_DMA2_CLK_DISABLE()

#define OV2640_DMA DMA2
#define OV2640_DMA_STREAM DMA1_Stream4
#define OV2640_DMA_IRQn DMA1_Stream4_IRQn
#define OV2640_DMA_IRQHandler DMA1_Stream4_IRQHandler

#define OV2640_PIXCLK_GPIO_PORT GPIOA
#define OV2640_PIXCLK_GPIO_PIN GPIO_PIN_6

#define OV2640_HSYNC_GPIO_PORT GPIOA
#define OV2640_HSYNC_GPIO_PIN GPIO_PIN_4

#define OV2640_VSYNC_GPIO_PORT GPIOB
#define OV2640_VSYNC_GPIO_PIN GPIO_PIN_7

#define OV2640_DCMI0_GPIO_PORT GPIOC
#define OV2640_DCMI0_GPIO_PIN GPIO_PIN_6

#define OV2640_DCMI1_GPIO_PORT GPIOC
#define OV2640_DCMI1_GPIO_PIN GPIO_PIN_7

#define OV2640_DCMI2_GPIO_PORT GPIOC
#define OV2640_DCMI2_GPIO_PIN GPIO_PIN_8

#define OV2640_DCMI3_GPIO_PORT GPIOC
#define OV2640_DCMI3_GPIO_PIN GPIO_PIN_9

#define OV2640_DCMI4_GPIO_PORT GPIOE
#define OV2640_DCMI4_GPIO_PIN GPIO_PIN_4

#define OV2640_DCMI5_GPIO_PORT GPIOB
#define OV2640_DCMI5_GPIO_PIN GPIO_PIN_6

#define OV2640_DCMI6_GPIO_PORT GPIOE
#define OV2640_DCMI6_GPIO_PIN GPIO_PIN_5

#define OV2640_DCMI7_GPIO_PORT GPIOE
#define OV2640_DCMI7_GPIO_PIN GPIO_PIN_6

#define OV2640_GPIO_CLK_ENABLE()      \
    {                                 \
        __HAL_RCC_GPIOA_CLK_ENABLE(); \
        __HAL_RCC_GPIOB_CLK_ENABLE(); \
        __HAL_RCC_GPIOC_CLK_ENABLE(); \
        __HAL_RCC_GPIOE_CLK_ENABLE(); \
    }

#define OV2640_DCMI_IT_PRIORITY 13U

/* Definition for OV2640_I2C clock resources */
#define OV2640_I2C I2C1
#define OV2640_I2C_CLK_ENABLE() __HAL_RCC_I2C1_CLK_ENABLE()
#define OV2640_I2C_CLK_DISABLE() __HAL_RCC_I2C1_CLK_DISABLE()
#define OV2640_I2C_SDA_GPIO_CLK_ENABLE() __HAL_RCC_GPIOB_CLK_ENABLE()
#define OV2640_I2C_SCL_GPIO_CLK_ENABLE() __HAL_RCC_GPIOB_CLK_ENABLE()

#define OV2640_I2C_FORCE_RESET() __HAL_RCC_I2C1_FORCE_RESET()
#define OV2640_I2C_RELEASE_RESET() __HAL_RCC_I2C1_RELEASE_RESET()

/* Definition for OV2640_I2C Pins */
#define OV2640_I2C_SCL_PIN GPIO_PIN_8
#define OV2640_I2C_SCL_GPIO_PORT GPIOB
#define OV2640_I2C_SDA_PIN GPIO_PIN_9
#define OV2640_I2C_SDA_GPIO_PORT GPIOB
#define OV2640_I2C_SCL_SDA_AF GPIO_AF4_I2C1

/* Definition for OV2640_I2C's NVIC */
#define OV2640_I2C_EV_IRQn I2C1_EV_IRQn
#define OV2640_I2C_ER_IRQn I2C1_ER_IRQn
#define OV2640_I2C_EV_IRQHandler I2C1_EV_IRQHandler
#define OV2640_I2C_ER_IRQHandler I2C1_ER_IRQHandler

#define OV2640_I2C_ADDRESS 0x78

/* I2C TIMING Register define when I2C clock source is APB1 (SYSCLK/4) */
/* I2C TIMING is calculated in case of the I2C Clock source is the APB1CLK = 120 MHz */
/* This example use TIMING to 0xc080242f to reach 100 kHz speed (Rise time = 640 ns, Fall time = 20 ns) */
/* This example use TIMING to 0xa031050f to reach 400 kHz speed (Rise time = 250 ns, Fall time = 100 ns) */
/* This example use TIMING to 0x00d3173c to reach 1 MHz speed (Rise time = 250 ns, Fall time = 100 ns) */
#define OV2640_I2C_TIMING 0xc080242f

#define OV2640_I2C_FREQUENCY 100000U /* Frequency of I2Cn = 100 KHz*/

#define OV2640_I2C_SUCCESS 0
#define OV2640_I2C_ERROR -1

#define OV2640_I2C_DIGITAL_FILTER_COEF 0U

#endif